package leetcode100;

import java.util.HashMap;
import java.util.Map;

public class lc025 {
    public static void main(String[] args) {
        String s = "pwwkewwww";
        int length = new lc025().lengthOfLongestSubstring(s);
        System.out.println("length = " + length);
    }



    public int lengthOfLongestSubstring(String s) {
        /**
         * Integer记录索引, Character是当前字符
         *
         *
         */
        int maxLength=0;
        int curLength=0;
        int start=0;
        Map<Character,Integer> map = new HashMap<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
//            System.out.println(map);
            char c = chars[i];
            if (map.containsKey(c)) {
//                int index = map.get(c);
//                map.values().removeIf(v->v<=index);
                start = Math.max(start,map.get(c)+1);
            }
            map.put(c,i);
            curLength = i-start+1;
            maxLength = Math.max(maxLength,curLength);
        }
        return maxLength;
    }
}
